﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalesSys.Comm.Utility
{
    public enum ReState
    {
        Success = 1,
        Unknown = 0,
        /// <summary>
        /// 发生错误
        /// </summary>
        Error = -1,
        ArgError = -2,
        RequestError = -3,
        InternalError = -4,
        /// <summary>
        /// 处理失败
        /// </summary>
        Fail = -10,
        /// <summary>
        /// 无权限
        /// </summary>
        Unauthorized = -20,
        /// <summary>
        /// 未登录
        /// </summary>

        NotLogged = -21
    }

    public static class ResultStateEx
    {
        public static ResultState<T> CreateResult<T>(this ReState state, string msg, T data)
        {
            return ResultState.Create(msg, state, data);
        }
        public static ResultState Create(this ReState state, string msg = null)
        {
            return new ResultState() { State = (int)state, Msg = msg };
        }
    }
    public class ResultState<T> : ResultState, IResultState<T>
    {
        public T Data { get; set; }
    }
    public interface IResultState<T> : IResultState
    {
        T Data { get; set; }
    }
    public class ResultState : IResultState
    {
        public static ResultState<T> Create<T>(string msg, ReState state, T data)
        {
            return new ResultState<T>() { Msg = msg, State = (int)state, Data = data };
        }
        public static ResultState<T> Create<T>(KeyValuePair<bool, string> kv, T data)
        {
            return new ResultState<T>() { State = (int)(kv.Key ? ReState.Success : ReState.Error), Msg = kv.Value, Data = data };
        }
        public static ResultState CreateSuccess(string msg = null)
        {
            return new ResultState() { State = (int)ReState.Success, Msg = msg };
        }
        public static ResultState<T> CreateSuccess<T>(T data, string msg = null)
        {
            return new ResultState<T>() { Data = data, State = (int)ReState.Success, Msg = msg };
        }
        public static ResultState Create(KeyValuePair<bool, string> kv)
        {
            return new ResultState() { State = (int)(kv.Key ? ReState.Success : ReState.Error), Msg = kv.Value };
        }

        /// <summary>
        /// 运行状态
        /// </summary>
        public virtual int State { get; set; }
        /// <summary>
        /// 相关信息
        /// </summary>
        public virtual string Msg { get; set; }

        public void CopyState(ResultState rs)
        {
            State = rs.State;
            Msg = rs.Msg;
        }
        public override string ToString()
        {
            return Msg;
        }
    }

    public interface IResultState
    {
        int State { get; set; }
        /// <summary>
        /// 相关信息
        /// </summary>
        string Msg { get; set; }
    }

}
